package com.shr.lease.web.admin.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shr.lease.common.exception.LeaseException;
import com.shr.lease.common.result.Result;
import com.shr.lease.common.result.ResultCodeEnum;
import com.shr.lease.model.entity.SystemPost;
import com.shr.lease.model.entity.SystemUser;
import com.shr.lease.model.enums.BaseStatus;
import com.shr.lease.web.admin.service.SystemPostService;
import com.shr.lease.web.admin.service.SystemUserService;
import com.shr.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.shr.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Resource
    private SystemUserService systemUserService;
    @Resource
    private SystemPostService systemPostService;


    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        IPage<SystemUserItemVo> page = systemUserService.pageSystemUserItemVo(
                new Page<SystemUserItemVo>(current, size),
                queryVo
        );
        return Result.ok(page);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUser systemUser = systemUserService.getById(id);
        SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = SystemUserItemVo
                .builder()
                .postName(systemPost.getName())
                .build();
        BeanUtils.copyProperties(systemUser, systemUserItemVo);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        //判断要更新或插入的用户名是否和已有的用户名冲突
        long count;
        if (systemUser.getId() == null) {//插入
            count = systemUserService.count(
                    new LambdaQueryWrapper<SystemUser>()
                            .eq(SystemUser::getUsername, systemUser.getUsername())
            );
        } else {//更新
            count = systemUserService.count(
                    new LambdaQueryWrapper<SystemUser>()
                            .eq(SystemUser::getUsername, systemUser.getUsername())
                            .ne(SystemUser::getId, systemUser.getId())
            );
        }
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR);
        }
        //密码不为空且不为空字符 则需要加密密码
        //systemUser.getPassword() != null && !systemUser.getPassword().isEmpty()
        if (!StringUtils.isBlank(systemUser.getPassword())) {
            systemUser.setPassword(DigestUtils.sha256Hex(systemUser.getPassword()));
        }
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        long count = systemUserService.count(
                new LambdaQueryWrapper<SystemUser>()
                        .like(SystemUser::getUsername, username)
        );
        return Result.ok(count == 0);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        systemUserService.update(
                new LambdaUpdateWrapper<SystemUser>()
                        .eq(SystemUser::getId, id)
                        .set(SystemUser::getStatus, status)
        );
        return Result.ok();

    }
}
